<?php
/**
 * 错误处理类
 * User: minyifei
 * Date: 15/7/8
 * Time: 下午7:37
 */

namespace MyfApi\Libs;


class ErrorCode {

    const SUCCESS = 0;
    const FAIL = 1;
    const PARAM_ERROR = 2;
    const TOKEN_ERROR = 3;
    const SIGNATURE_ERROR = 4;
    const SQL_ERROR = 5;

    public static $codes = array(
        self::SUCCESS => "操作成功",
        self::FAIL => "出错了",
        self::PARAM_ERROR => "参数错误",
        self::TOKEN_ERROR => "TOKEN失效",
        self::SIGNATURE_ERROR => "签名错误",
        self::SQL_ERROR => '系统数据异常',
    );

    /**
     * 报异常
     * @param String $msg
     * @param int $code
     * @throws \Exception
     */
    public static function throwException($msg,$code=ErrorCode::FAIL) {
        if (!isset($msg) && array_key_exists($code, self::$codes)) {
            $msg = self::$codes[$code];
        }
        throw new \Exception($msg, $code);
    }

    /**
     * 报异常
     * @param int $code 已定义的异常code
     * @throws \Exception
     */
    public static function throwExceptionCode($code=ErrorCode::FAIL){
        self::throwException(null,$code);
    }
}